package com.web.pendingList.service;

import com.web.account.entity.Account;
import com.web.account.service.AccountService;
import com.web.common.BaseResult;
import com.web.common.PendingAmend;
import com.web.common.SpringUtils;
import com.web.common.enums.BaseStatus;
import com.web.common.enums.PendingAction;
import com.web.contract.entity.Contract;
import com.web.contract.service.ContractService;
import com.web.customer.entity.Customer;
import com.web.customer.service.CustomerService;
import com.web.pendingList.entity.PendingList;
import com.web.pendingList.mapper.PendingListMapper;
import com.web.qualification.entity.CustomerQualification;
import com.web.qualification.service.CustomerQualificationService;
import com.web.systemParam.entity.SystemParam;
import com.web.systemParam.service.SystemParamService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.sound.midi.Soundbank;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * @author wenzhuo
 * @create 2024-03-13 2024/3/13
 */
@Service
public class PendingListService {

    @Resource
    private CustomerService customerService;

    @Resource
    private AccountService accountService;

    @Resource
    private SystemParamService systemParamService;

    @Resource
    private ContractService contractService;

    @Resource
    private CustomerQualificationService qualificationService;

    @Resource
    private PendingListMapper pendingListMapper;


    public List<PendingList> getPendingList(String status, String txType) {
        return pendingListMapper.getPendingListByStatusTxType(status, txType);
    }

    public Integer addPendingTxn(PendingList pendingTxn) {
        return pendingListMapper.addPendingTransaction(pendingTxn);
    }

    public PendingList getInPending(Integer pendingNumber) {
        return pendingListMapper.getTxnByPendingNumber(pendingNumber);
    }

    public Integer updatePendingTxn(PendingList pendingTxn) {
        return pendingListMapper.updatePendingByNumber(pendingTxn);
    }

    @Transactional
    public BaseResult addCustomerPending(String txType, String module, String maker, Customer customer) {
        PendingList pendingTxn = new PendingList();
        pendingTxn.setTxType(txType);
        pendingTxn.setModule(module);
        pendingTxn.setMaker(maker);
        pendingTxn.setRequestType(generateCustomerRequestType(txType, module, customer, null));

        //新增审批记录成功之后获取pendingNumber
        Integer pendingNumber = createPending(pendingTxn);
        if(pendingNumber <= 0){
            return new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(), BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        customer.setPendingNumber(pendingNumber);

        //新增交易的Pending需要在Customer表中新增記錄
        if (PendingAction.ADD.getAction().equals(pendingTxn.getTxType())) {
            //生成Customer数据，APPROVED='N',页面不会查询出来
            if (customerService.addCustomer(customer) <= 0) {
                return new BaseResult<>(BaseStatus.FCE_INSERT_FAIL.getCode(), BaseStatus.FCE_INSERT_FAIL.getDesc());
            }
            return new BaseResult<>(BaseStatus.SUCCESS.getCode(), BaseStatus.FCE_INSERT_SUCCESS.getDesc());
        }

        //將生成的PendingNumber賦值給Customer
        if (customerService.updatePendingNumber(customer) <= 0) {
            return new BaseResult<>(BaseStatus.FCE_UPDATE_FAIL.getCode(), BaseStatus.FCE_UPDATE_FAIL.getDesc());
        }

        return new BaseResult<>(BaseStatus.SUCCESS.getCode(), BaseStatus.FCE_INSERT_SUCCESS.getDesc());
    }

    //生成CustomerRequestType中的内容
    private String generateCustomerRequestType(String txType, String module, Customer customerOld, Customer CustomerNew) {
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder baseBuilder = stringBuilder.append(PendingAction.generateDescription(txType)).append(" ").append(module).append("(")
                .append("customerName: ").append(customerOld.getCustomerName()).append(",");

        if (PendingAction.MOD.getAction().equals(txType)) {
            baseBuilder.append("customerGroup:").append(customerOld.getCustomerGroup()).append(" —> ").append(CustomerNew.getCustomerGroup());
        } else {
            baseBuilder.append("customerGroup:").append(customerOld.getCustomerGroup());
        }
        return baseBuilder.append(")").toString().toLowerCase();
    }

    @Transactional
    public BaseResult handleModifyCustomer(Customer customerOld, Customer customerNew, String userName) {
        //生成一笔审批记录并且获取返回的PendingNumber
        PendingList pendingTxn = new PendingList();
        pendingTxn.setTxType(PendingAction.MOD.getAction());
        pendingTxn.setModule(Customer.MODULE);
        pendingTxn.setMaker(userName);
        pendingTxn.setRequestType(generateCustomerRequestType(pendingTxn.getTxType(), pendingTxn.getModule(), customerOld, customerNew));

        Integer pendingNumber = createPending(pendingTxn);
        if(pendingNumber <= 0){
            return new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(), BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        //将返回的pendingNumber赋值给CustomerOld.
        customerOld.setPendingNumber(pendingNumber);
        if (customerService.updatePendingNumber(customerOld) <= 0) {
            return new BaseResult<>(BaseStatus.FCE_UPDATE_FAIL.getCode(), BaseStatus.FCE_UPDATE_FAIL.getDesc());
        }
        ;

        //将修改的CustomerNew数据存入临时表中
        String customerGroupNew = customerNew.getCustomerGroup();
        BeanUtils.copyProperties(customerOld, customerNew);
        customerNew.setCustomerGroup(customerGroupNew);
        if (customerService.addTemporaryData(customerNew) <= 0) {
            return new BaseResult<>(BaseStatus.FCE_INSERT_FAIL.getCode(), BaseStatus.FCE_INSERT_FAIL.getDesc());
        };

        return new BaseResult(BaseStatus.SUCCESS.getCode(), BaseStatus.FCE_UPDATE_SUCCESS.getDesc());

    }

    @Transactional
    public BaseResult addAccountPending(Account account, String userName, String module, String TxType) {
        //生成Pending
        PendingList pendingTxn = new PendingList();
        pendingTxn.setTxType(TxType);
        pendingTxn.setModule(module);
        pendingTxn.setMaker(userName);
        pendingTxn.setRequestType(genAccountRequestType(pendingTxn.getTxType(), pendingTxn.getModule(), account, null));

        //新增审批记录成功之后获取pendingNumber
        Integer pendingNumber = createPending(pendingTxn);
        if(pendingNumber <= 0){
            return new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(), BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        account.setPendingNumber(pendingNumber);
        if (PendingAction.ADD.getAction().equals(pendingTxn.getTxType())) {
            //生成Account数据
            if (accountService.addAccount(account) <= 0) {
                return new BaseResult<>(BaseStatus.FCE_INSERT_FAIL.getCode(), BaseStatus.FCE_INSERT_FAIL.getDesc());
            }
            ;
            return new BaseResult(BaseStatus.SUCCESS.getCode(), BaseStatus.FCE_INSERT_SUCCESS.getDesc());
        }

        //将Account的PendingNumber进行赋值.
        if (accountService.updateAccount(account) <= 0) {
            return new BaseResult<>(BaseStatus.FCE_UPDATE_FAIL.getCode(), BaseStatus.FCE_UPDATE_FAIL.getDesc());
        }
        return new BaseResult<>(BaseStatus.SUCCESS.getCode(), BaseStatus.FCE_INSERT_SUCCESS.getDesc());


    }

    private String genAccountRequestType(String txType, String module, Account accountOld, Account accountNew) {
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder baseBuilder = stringBuilder.append(PendingAction.generateDescription(txType)).append(" ").append(module.toLowerCase()).append("(")
                .append("currency: ").append(accountOld.getCurrency()).append(", ")
                .append("accountType: ").append(accountOld.getAccountType()).append(", ")
                .append("customerNo: ").append(accountOld.getCustomerNo()).append(", ")
                .append("remark: ").append(accountOld.getRemark());

        if (PendingAction.MOD.getAction().equals(txType)) {
            if (accountOld.getAccountBalance().compareTo(accountOld.getAccountBalance()) == 0) {
                baseBuilder.append(" —> ").append(accountNew.getRemark()).append(",")
                        .append("accountBalance:").append(accountOld.getAccountBalance())
                        .append(" —> ").append(accountNew.getAccountBalance());
            }

        }
        return baseBuilder.append(")").toString();
    }

    public BaseResult handleModifyAccount(Account accountOld, Account accountNew, String userName) {

        //生成一笔审批记录并且获取返回的PendingNumber
        PendingList pendingTxn = new PendingList();
        pendingTxn.setTxType(PendingAction.MOD.getAction());
        pendingTxn.setModule(Account.MODULE);
        pendingTxn.setMaker(userName);
        pendingTxn.setRequestType(genAccountRequestType(pendingTxn.getTxType(), pendingTxn.getModule(), accountOld, accountNew));

        //新增审批记录成功之后获取pendingNumber
        Integer pendingNumber = createPending(pendingTxn);
        if(pendingNumber <= 0){
            return new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(), BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        //将返回的pendingNumber赋值给accountOld.
        accountOld.setPendingNumber(pendingNumber);
        if (accountService.ModifyPendingNumber(accountOld) <= 0) {
            return new BaseResult<>(BaseStatus.FCE_UPDATE_FAIL.getCode(), BaseStatus.FCE_UPDATE_FAIL.getDesc());
        }

        //将修改的accountNew数据存入临时表中
        String remark = accountNew.getRemark();
        BigDecimal accountBalance = accountNew.getAccountBalance();

        BeanUtils.copyProperties(accountOld, accountNew);

        accountNew.setRemark(remark);
        accountNew.setAccountBalance(accountBalance);
        if (accountService.addTemporaryData(accountNew) <= 0) {
            return new BaseResult<>(BaseStatus.FCE_INSERT_FAIL.getCode(), BaseStatus.FCE_INSERT_FAIL.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(), BaseStatus.FCE_UPDATE_SUCCESS.getDesc());
    }

    public BaseResult addSystemParamPending(SystemParam systemParam, String userName, String module, String TxType) {

        //生成Pending
        PendingList pendingTxn = new PendingList();
        pendingTxn.setTxType(TxType);
        pendingTxn.setModule(module);
        pendingTxn.setMaker(userName);
        pendingTxn.setRequestType(genSystemParamRequestType(pendingTxn.getTxType(), pendingTxn.getModule(), systemParam, null));

        //新增审批记录成功之后获取pendingNumber
        Integer pendingNumber = createPending(pendingTxn);
        if(pendingNumber <= 0){
            return new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(), BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        systemParam.setPendingNumber(pendingNumber);
        if (PendingAction.ADD.getAction().equals(pendingTxn.getTxType())) {
            //生成System数据
            if (systemParamService.addSystemParam(systemParam) <= 0) {
                return new BaseResult<>(BaseStatus.FCE_INSERT_FAIL.getCode(), BaseStatus.FCE_INSERT_FAIL.getDesc());
            }
            return new BaseResult(BaseStatus.SUCCESS.getCode(), BaseStatus.FCE_INSERT_SUCCESS.getDesc());
        }

        //将systemParam的PendingNumber进行赋值.
        if (systemParamService.updatePendingNumber(systemParam) <= 0) {
            return new BaseResult<>(BaseStatus.FCE_UPDATE_FAIL.getCode(), BaseStatus.FCE_UPDATE_FAIL.getDesc());
        }

        return new BaseResult<>(BaseStatus.SUCCESS.getCode(), BaseStatus.FCE_INSERT_SUCCESS.getDesc());

    }

    private String genSystemParamRequestType(String txType, String module, SystemParam systemParamOld, SystemParam systemParamNew) {
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder baseBuilder = stringBuilder.append(PendingAction.generateDescription(txType)).append(" ").append(module.toLowerCase()).append("(")
                .append("systemParamName: ").append(systemParamOld.getSystemParamName()).append(", ")
                .append("systemParamType: ").append(systemParamOld.getSystemParamType()).append(", ");

        if(PendingAction.MOD.getAction().equals(txType)){
            baseBuilder.append("systemParamValue: ").append(systemParamOld.getSystemParamValue()).append(" —> ")
                    .append(systemParamNew.getSystemParamValue()).append(",").append("remark: ").append(systemParamOld.getRemark())
                    .append(" —> ").append(systemParamNew.getRemark());
        }else {
            baseBuilder.append("systemParamValue: ").append(systemParamOld.getSystemParamValue()).append(", ")
                    .append("remark: ").append(systemParamOld.getRemark());
        }

        return baseBuilder.append(")").toString();

    }

    public BaseResult handleModifySystemParam(SystemParam systemParamOld, SystemParam systemParamNew, String userName) {
        //生成一笔审批记录并且获取返回的PendingNumber
        PendingList pendingTxn = new PendingList();
        pendingTxn.setTxType(PendingAction.MOD.getAction());
        pendingTxn.setModule(SystemParam.MODULE);
        pendingTxn.setMaker(userName);
        pendingTxn.setRequestType(genSystemParamRequestType(pendingTxn.getTxType(), pendingTxn.getModule(), systemParamOld, systemParamNew));


        Integer pendingNumber = createPending(pendingTxn);
        if(pendingNumber <= 0){
            return new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(), BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        //将返回的pendingNumber赋值给systemParamOld.
        systemParamOld.setPendingNumber(pendingNumber);
        if (systemParamService.updatePendingNumber(systemParamOld) <= 0) {
            return new BaseResult<>(BaseStatus.FCE_UPDATE_FAIL.getCode(), BaseStatus.FCE_UPDATE_FAIL.getDesc());
        }

        //使用BeanUtils复制一份和systemParamOld一样的systemParamNew出来
        String remark = systemParamNew.getRemark();
        String systemParamValue = systemParamNew.getSystemParamValue();

        BeanUtils.copyProperties(systemParamOld, systemParamNew);

        systemParamNew.setRemark(remark);
        systemParamNew.setSystemParamValue(systemParamValue);
        //将修改的accountNew数据存入临时表中
        if (systemParamService.addTemporaryData(systemParamNew) <= 0) {
            return new BaseResult<>(BaseStatus.FCE_INSERT_FAIL.getCode(), BaseStatus.FCE_INSERT_FAIL.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(), BaseStatus.FCE_UPDATE_SUCCESS.getDesc());

    }


    private Integer createPending(PendingList pendingTxn) {
        Integer pendingNumber = 0;

        try {
            pendingListMapper.addPendingTransaction(pendingTxn);
            pendingNumber = pendingTxn.getPendingNum();
        } catch (Exception e) {
            throw e;
        }
        if (pendingNumber == 0) {
            return -1;
        }
        return pendingNumber;
    }

    public PendingAmend checkModule(String module) {
        PendingAmend pendingAmend = null;

        switch (module){
            case Customer.MODULE: pendingAmend = SpringUtils.getBean(CustomerService.class);break;
            case Account.MODULE: pendingAmend = SpringUtils.getBean(AccountService.class);break;
            case SystemParam.MODULE: pendingAmend = SpringUtils.getBean(SystemParamService.class);break;
            case CustomerQualification.MODULE: pendingAmend = SpringUtils.getBean(CustomerQualificationService.class);break;
            case Contract.MODULE: pendingAmend = SpringUtils.getBean(ContractService.class);break;
            default: break;
        }
        return pendingAmend;
    }

   @Transactional
    public BaseResult addQualificationPending(String txType, String module, String maker,List<CustomerQualification> qualifications) {
        //新增pending数据
       for (CustomerQualification qualification : qualifications) {
           PendingList pendingTxn = new PendingList();
           pendingTxn.setTxType(txType);
           pendingTxn.setModule(module);
           pendingTxn.setMaker(maker);
           pendingTxn.setRequestType(generateQualificationRequestType(txType, module, qualification, null));

           //新增审批记录成功之后获取pendingNumber
           Integer pendingNumber = createPending(pendingTxn);
           if(pendingNumber <= 0){
               return new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(), BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
           }

           qualification.setPendingNumber(pendingNumber);
       }

       //批量生成CustomerQualification数据
       if(qualificationService.generateQualificationBatch(qualifications) <= 0) {
           return new BaseResult(BaseStatus.INSERT_QUALIFICATION_FAILED.getCode(),BaseStatus.INSERT_QUALIFICATION_FAILED.getDesc());
       }

       return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.SUCCESS.getDesc());

    }

    private String generateQualificationRequestType(String txType, String module, CustomerQualification qualificationNew, CustomerQualification qualificationOld) {
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder baseBuilder = stringBuilder.append(PendingAction.generateDescription(txType)).append(" ").append(module.toLowerCase()).append("(");

       if(txType.equals(PendingAction.MOD.getAction())){
           baseBuilder.append("customerNo: ").append(qualificationOld.getCustomerNo()).append("—>").append(qualificationNew.getCustomerNo()).append(",")
                   .append("customerName: ").append(qualificationOld.getCustomerName()).append("—>").append(qualificationNew.getCustomerName()).append(", ")
                   .append("professionalInvestor: ").append(qualificationOld.getProfessionalInvestor()).append("—>").append(qualificationNew.getProfessionalInvestor()).append(", ")
                   .append("expirationDate: ").append(qualificationOld.getExpirationDate()).append("—>").append(qualificationNew.getExpirationDate()).append(", ")
                   .append("allowedOption: ").append(qualificationOld.getAllowedOption()).append("—>").append(qualificationNew.getAllowedOption()).append(", ")
                   .append("allowedBookRate: ").append(qualificationOld.getAllowedBookRate()).append("—>").append(qualificationNew.getAllowedBookRate()).append(", ")
                   .append("creditRating: ").append(qualificationOld.getCreditRating()).append("—>").append(qualificationNew.getCreditRating()).append(", ")
                   .append("assetLevel: ").append(qualificationOld.getAssetLevel()).append("—>").append(qualificationNew.getAssetLevel());
       }else{
           baseBuilder.append("customerNo: ").append(qualificationNew.getCustomerNo()).append(", ")
                   .append("customerName: ").append(qualificationNew.getCustomerName()).append(", ")
                   .append("professionalInvestor: ").append(qualificationNew.getProfessionalInvestor()).append(", ")
                   .append("expirationDate: ").append(qualificationNew.getExpirationDate()).append(", ")
                   .append("allowedOption: ").append(qualificationNew.getAllowedOption()).append(", ")
                   .append("allowedBookRate: ").append(qualificationNew.getAllowedBookRate()).append(", ")
                   .append("creditRating: ").append(qualificationNew.getCreditRating()).append(", ")
                   .append("loanCeiling: ").append(qualificationNew.getLoanCeiling()).append(", ")
                   .append("primeRate: ").append(qualificationNew.getPrimeRate()).append(", ")
                   .append("assetLevel: ").append(qualificationNew.getAssetLevel());
       }


        return baseBuilder.append(")").toString();

    }

    @Transactional
    public BaseResult deleteQualificationPending(String txType, String module, String maker, CustomerQualification qualification) {
        //新增pending数据
        PendingList pendingTxn = new PendingList();
        pendingTxn.setTxType(txType);
        pendingTxn.setModule(module);
        pendingTxn.setMaker(maker);
        pendingTxn.setRequestType(generateQualificationRequestType(txType, module, qualification, null));

        //新增审批记录成功之后获取pendingNumber
        Integer pendingNumber = createPending(pendingTxn);
        if(pendingNumber <= 0){
            return new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(), BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        qualification.setPendingNumber(pendingNumber);

        //将Qualification数据的PendingNumber存入数据库
        if(qualificationService.updatePendingNumber(qualification) <= 0){
            return new BaseResult<>(BaseStatus.QUALIFICATION__DATA_EXCEPTION.getCode(), BaseStatus.QUALIFICATION__DATA_EXCEPTION.getDesc());
        }

        return new BaseResult<>(BaseStatus.SUCCESS.getCode(), BaseStatus.FCE_DELETE_SUCCESS.getDesc());
    }

    @Transactional
    public BaseResult handleModifyQualification(CustomerQualification qualificationOld, CustomerQualification qualificationNew, String userName) {
        //生成一笔审批记录并且获取返回的PendingNumber
        PendingList pendingTxn = new PendingList();
        pendingTxn.setTxType(PendingAction.MOD.getAction());
        pendingTxn.setModule(CustomerQualification.MODULE);
        pendingTxn.setMaker(userName);
        pendingTxn.setRequestType(generateQualificationRequestType(pendingTxn.getTxType(), pendingTxn.getModule(), qualificationNew, qualificationOld));

        Integer pendingNumber = createPending(pendingTxn);
        if(pendingNumber <= 0){
            return new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(), BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        //将返回的pendingNumber赋值给qualificationOld.
        qualificationOld.setPendingNumber(pendingNumber);
        if (qualificationService.updatePendingNumber(qualificationOld) <= 0) {
            return new BaseResult<>(BaseStatus.FCE_UPDATE_FAIL.getCode(), BaseStatus.FCE_UPDATE_FAIL.getDesc());
        };

        //将修改的qualificationNew数据存入临时表中
        Integer customerNo = qualificationNew.getCustomerNo();
        String customerName = qualificationNew.getCustomerName();
        LocalDate expirationDate = qualificationNew.getExpirationDate();
        String professionalInvestor = qualificationNew.getProfessionalInvestor();
        String allowedOption = qualificationNew.getAllowedOption();
        String allowedBookRate = qualificationNew.getAllowedBookRate();
        String creditRating = qualificationNew.getCreditRating();
        String assetLevel = qualificationNew.getAssetLevel();


        BeanUtils.copyProperties(qualificationOld, qualificationNew);

        qualificationNew.setCustomerNo(customerNo);
        qualificationNew.setCustomerName(customerName);
        qualificationNew.setExpirationDate(expirationDate);
        qualificationNew.setProfessionalInvestor(professionalInvestor);
        qualificationNew.setAllowedOption(allowedOption);
        qualificationNew.setAllowedBookRate(allowedBookRate);
        qualificationNew.setCreditRating(creditRating);
        qualificationNew.setAssetLevel(assetLevel);

        if (qualificationService.addTemporaryData(qualificationNew) <= 0) {
            return new BaseResult<>(BaseStatus.FCE_INSERT_FAIL.getCode(), BaseStatus.FCE_INSERT_FAIL.getDesc());
        };

        return new BaseResult(BaseStatus.SUCCESS.getCode(), BaseStatus.FCE_UPDATE_SUCCESS.getDesc());

    }

    @Transactional
    public BaseResult addContractPending(String txType, String module, String maker, Contract contract) {
        PendingList pendingTxn = new PendingList();
        pendingTxn.setTxType(txType);
        pendingTxn.setModule(module);
        pendingTxn.setMaker(maker);
        pendingTxn.setRequestType(generateContractRequestType(txType, module, contract));

        //新增审批记录成功之后获取pendingNumber
        Integer pendingNumber = createPending(pendingTxn);
        if(pendingNumber <= 0){
            return new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(), BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        contract.setPendingNumber(pendingNumber);

        //新增交易的Pending需要在Contract表中新增記錄
        if (PendingAction.ADD.getAction().equals(pendingTxn.getTxType())) {
            //生成Contract数据，APPROVED='N',页面不会查询出来
            if (contractService.generateContract(contract) <= 0) {
                return new BaseResult<>(BaseStatus.FCE_INSERT_FAIL.getCode(), BaseStatus.FCE_INSERT_FAIL.getDesc());
            }
            return new BaseResult<>(BaseStatus.SUCCESS.getCode(), BaseStatus.FCE_INSERT_SUCCESS.getDesc());
        }

        return new BaseResult<>(BaseStatus.SUCCESS.getCode(), BaseStatus.FCE_INSERT_SUCCESS.getDesc());
    }

    private String generateContractRequestType(String txType, String module, Contract contract) {
            StringBuilder stringBuilder = new StringBuilder();
            StringBuilder baseBuilder = stringBuilder.append(PendingAction.generateDescription(txType)).append(" ").append(module.toLowerCase()).append("(");

            baseBuilder.append("customerNo: ").append(contract.getCustomerNo()).append(", ")
                    .append("customerName: ").append(contract.getCustomerName()).append(", ")
                    .append("buyCurrency: ").append(contract.getBuyCurrency()).append(", ")
                    .append("sellCurrency: ").append(contract.getSellCurrency()).append(", ")
                    .append("costRate: ").append(contract.getCostRate()).append(", ")
                    .append("acceptedBooKRate: ").append(contract.getAcceptedBooKRate()).append(", ")
                    .append("buyAmount: ").append(contract.getBuyAmount()).append(", ")
                    .append("sellAmount: ").append(contract.getSellAmount()).append(", ")
                    .append("settlementDate: ").append(contract.getSettlementDate());

            return baseBuilder.append(")").toString();


    }
}
